//类里面声明，类外面定义
#include"Date.h"

Date::Date(int year,int month,int day)//Date成员函数
{
    _year=year;
    _month=month;
    _day=day;
    if(CheckDate())
    {
        cout<<"日期非法"<<endl;
        cout<<*this;
    }
}

void Date::print()
{
    cout<<_year<<"-"<<_month<<"-"<<_day<<endl;
}

bool Date::operator<(const Date& d)
{
    if(_year<d._year)
        return true;
    else if(_year==d._year&&_month<d._month)
        return true;
    else if(_year==d._year
           &&_month==d._month
           &&_day<d._day)
        return true;
    return false;
}
bool Date::operator>(const Date& d)
{
    return !(*this<=d);
}
//d1<=d2,那么this就是d1的地址，d就是d2的地址
bool Date::operator<=(const Date& d)
{
    return *this<d||*this==d;
}
bool Date::operator>=(const Date& d)
{
    return !(*this<d);
}
bool Date::operator==(const Date& d)
{
    return _year==d._year&&
           _month==d._month&&
           _day==d._day;
}
bool Date::operator!=(const Date& d)
{
    return !(*this==d);
}

//d1+100
Date& Date::operator+=(int day)
{
    if(day<0)
    {
        return *this-=-day;
    }
    _day+=day;
    while(_day>GetMonthDay(_year,_month))
    {
        _day-=GetMonthDay(_year,_month);
        ++_month;
        if(_month==13)
        {
            _year++;
            _month=1;
        }
    }
    return *this;
}

Date& Date::operator+(int day)
{
    Date tmp=*this;
    tmp+=day;

    return tmp;
}

/*Date& Date::operator+(int day)
{
    Date tmp=*this;
    tmp._day+=day;
    while(tmp._day>GetMonthDay(tmp._year,_month))
    {
        tmp._day-=GetMonthDay(tmp._year,_month);
        ++_month;
        if(tmp._month==13)
        {
           tmp. _year++;
            tmp._month=1;
        }
    }
    return tmp;
}

//复用+
Date& Date::operator+=(int day)
{
    *this=*this+day;
    return *this;
}
*/

Date& Date::operator-=(int day)
{
    if(day<0)
    {
        return *this+=-day;
    }
    _day-=day;
    while(_day<=0)
    {
        --_month;
        if(_month==0)
        {
            _month=12;
            --_year;
        }
        _day+=GetMonthDay(_year,_month);
    }

    return *this;
}

Date& Date::operator-(int day)
{
    Date tmp(*this);
    tmp-=day;
    return tmp;
}  

Date& Date::operator++()
{
    *this+=1;
    return *this;
}
Date Date::operator++(int)
{
    Date tmp(*this);
    *this+=1;

    return tmp;
}

Date& Date::operator--()
{
    *this-=1;
    return *this;
}
Date Date::operator--(int)
{
    Date tmp(*this);
    *this-=1;
    
    return tmp;
}

//d1-d2
int Date::operator-(const Date& d)
{
     Date max=*this;
     Date min=d;
     int flag=1;
     if(*this<d)
     {
        max=d;
        min=*this;

        flag=-1;
     }
     int n=0;
     while(min!=max)
     {
        ++min;
        ++n;
     }
     return n*flag;
} 

// void Date::operator<<(ostream& out)
// {
//     out<<_year<<"年"<<_month<<"月"<<_day<<"日"<<endl;
// } 

ostream& operator<<(ostream& out,const Date& d)
{
    out << d._year << "年" << d._month << "月" << d._day << "日";
    return out;
}
istream& operator>>(istream& out,  Date& d)
{
    while(1){
        cout<<"请依次输入年月日:>";
        cin>>d._year>>d._month>>d._day;
        if(d.CheckDate())
        {
            break;
        }
        else
        {
            cout<<"日期非法，请重新输入"<<endl;
        }
    }
    return cin;
}